package com.example.javafx.bilibilidownload.util

import com.fasterxml.jackson.databind.ObjectMapper
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element

object HtmlUtil {

    @JvmStatic
    fun getVideoUrl(doc: Document): String {
        //拿到视频的script
        val jsonNode = ObjectMapper().readTree(
            doc?.getElementsByTag("script")
                ?.firstOrNull { it.html().contains("window.__playinfo__=") }
                ?.html()?.replace("window.__playinfo__=", "") ?: return ""
        )
        // 如果任意一个节点是 null，返回空字符串
        return jsonNode["data"]?.get("dash")?.get("video")?.get(0)?.get("baseUrl")?.asText() ?: ""

    }

    @JvmStatic
    fun getAudioUrl(doc: Document): String {
        val jsonNode = ObjectMapper().readTree(
            doc?.getElementsByTag("script")
                ?.firstOrNull { it.html().contains("window.__playinfo__=") }
                ?.html()?.replace("window.__playinfo__=", "") ?: return ""
        )
        return jsonNode["data"]?.get("dash")?.get("audio")?.get(0)?.get("baseUrl")?.asText() ?: ""
    }

}

fun main(){
   var jsonNode = ObjectMapper().readTree("");
    println(jsonNode)
}